package com.jtech.together.view.fragment;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jtech.together.R;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.RecommendUserContract;
import com.jtech.together.mvp.presenter.RecommendUserPresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.util.LocationSelect;
import com.jtech.together.view.activity.UserProfileActivity;
import com.jtech.together.view.adapter.RecommendUserAdapter;
import com.jtech.together.view.fragment.base.BaseFragment;
import com.jtechlib2.listener.OnItemClickListener;
import com.jtechlib2.listener.OnLoadListener;
import com.jtechlib2.view.JRecyclerView;
import com.jtechlib2.view.RecyclerHolder;
import com.jtechlib2.view.RefreshLayout;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

import butterknife.Bind;
import cn.bmob.v3.datatype.BmobGeoPoint;

/**
 * 推荐用户
 * Created by wuxubaiyang on 2017/5/10.
 */
public class RecommendUserFragment extends BaseFragment implements RecommendUserContract.View, RefreshLayout.OnRefreshListener,
        OnItemClickListener, OnLoadListener, RecommendUserAdapter.RecommendUserListener {
    @Bind(R.id.refreshlayout_recommend_user)
    RefreshLayout refreshLayout;
    @Bind(R.id.jrecyclerview_recommend_user)
    JRecyclerView jRecyclerView;

    private RecommendUserContract.Presenter presenter;
    private RecommendUserAdapter recommendUserAdapter;

    @Override
    public View createView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        return layoutInflater.inflate(R.layout.fragment_recommend_user, viewGroup, false);
    }

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new RecommendUserPresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        //设置layoutmanager
        jRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //开启加载更多
        jRecyclerView.setLoadMore(true);
        //设置适配器
        recommendUserAdapter = new RecommendUserAdapter(getActivity());
        jRecyclerView.setAdapter(recommendUserAdapter);
        //设置分页从第0页开始
        recommendUserAdapter.setStartPage(0);
        //设置下拉刷新监听
        refreshLayout.setOnRefreshListener(this);
        //设置加载更多监听
        jRecyclerView.setOnLoadListener(this);
        //设置适配器操作监听
        recommendUserAdapter.setRecommendUserListener(this);
        //设置item点击事件
        jRecyclerView.setOnItemClickListener(this);
    }

    @Override
    protected void loadData() {
        refreshLayout.startRefreshing();
    }

    public static RecommendUserFragment newInstance() {
        Bundle args = new Bundle();
        RecommendUserFragment fragment = new RecommendUserFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void loadSuccess(List<UserModel> userModels, boolean loadMore) {
        refreshLayout.refreshingComplete();
        if (userModels.size() > 0) {
            recommendUserAdapter.setDatas(userModels, loadMore);
            jRecyclerView.setLoadCompleteState();
        } else {
            jRecyclerView.setLoadFinishState();
        }
    }

    @Override
    public void loadFail(String error) {
        refreshLayout.refreshingComplete();
        jRecyclerView.setLoadCompleteState();
        Snackbar.make(jRecyclerView, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onLocationClick(UserModel userModel) {
        BmobGeoPoint location = userModel.getLocation();
        LocationSelect.showTarget(getActivity(), new double[]{location.getLatitude(), location.getLongitude()},
                userModel.getNickname() + "\t在这里!", userModel.getAddress());
    }

    @Override
    public void onItemClick(RecyclerHolder holder, View view, int position) {
        Bundle bundle = new Bundle();
        bundle.putString(UserProfileActivity.KEY_USER_OBJECT_ID,
                recommendUserAdapter.getItem(holder.getAdapterPosition()).getObjectId());
        ActivityJump.build(getActivity(), UserProfileActivity.class)
                .addBundle(bundle)
                .jump();
    }

    @Override
    public void onRefresh() {
        presenter.loadRecommendUser(recommendUserAdapter.getPage(false),
                recommendUserAdapter.getDisplayNumber(), false);
    }

    @Override
    public void loadMore() {
        presenter.loadRecommendUser(recommendUserAdapter.getPage(true),
                recommendUserAdapter.getDisplayNumber(), true);
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
    }
}